//
//  ShopTagCell.m
//  成都咖啡
//
//  Created by figaro on 16/3/29.
//  Copyright © 2016年 wenhao. All rights reserved.
//
/**
 *  这里从Bmob拿到objectId,通过它来拿到shopTag这个数组，这样方法更简单，更容易理解，这里和前面的情况不一样。上个collection的是拿到数组中的一个元素来找符合的条件。这里是直接拿表里某一行的一个shopTag这个数组。这里很不一样。
 *
 *  @return <#return value description#>
 */

#import "ShopTagCell.h"
#import "ZPShopTagCollectionCell.h"
#import "ShoppingName.h"
#import <BmobSDK/Bmob.h>

//用延展填属性
@interface ShopTagCell (){
    NSArray *shopTagData;
}
- (void)initUserInterface;/**< 初始化用户界面 */
- (void)initDataSource;/**< 初始化数据源 */
@end

@implementation ShopTagCell

- (void)awakeFromNib {
    [self initDataSource];
    [self initUserInterface];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - Init methods
- (void)initDataSource{
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //1.从Bmob拿到数据
    BmobQuery *tagQuery = [BmobQuery queryWithClassName:@"ShopInfo"];
    NSString *shopObjectIDString = [ShoppingName shareShoppingName].selectedObjectID;
    
    [tagQuery getObjectInBackgroundWithId:shopObjectIDString block:^(BmobObject *object, NSError *error) {
        if (error) {
            NSLog(@"error = %@",error);
        } else {
            
            shopTagData = [object objectForKey:@"shopTag"];
            //理解线程：因为你修改了数据源，页面是在initDataSource里面实现，所以页面有可能，注意是有可能是在页面加载到数据之前，就已经跳转了。所以有可能拿不到数据。（这里是在异步的线程里拿的数据，所以要想提高用户体验，就要在这里加提示。页面跳转的时候就用）
            [self.collectionView reloadData];
            
        }
    }];
        
}
- (void)initUserInterface{
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    _shopNameOfTag.text = [ShoppingName shareShoppingName].selectedTitle;
}


#pragma mark <UICollectionViewDataSource>

//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//
//    return 1;
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    
    return shopTagData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZPShopTagCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPShopTagCollectionCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    //2.配置collection的cell
    NSString *shopTagImageString = [shopTagData objectAtIndex:indexPath.row];
//    NSLog(@"shopTag = %@",shopTagImageString);
    cell.backgroundColor = [UIColor whiteColor];
    cell.ShopTagImage.image = [UIImage imageNamed:shopTagImageString];
    
    
    return cell;
}

@end
